package com.yanqu.road.server.cmd.activity.gashapon;

import com.yanqu.road.message.YanQuMessage;
import com.yanqu.road.pb.activity.GashaponProto;
import com.yanqu.road.server.cmd.base.AbstractCommand;
import com.yanqu.road.server.command.Cmd;
import com.yanqu.road.server.gameplayer.GamePlayer;
import com.yanqu.road.server.gameplayer.module.activity.gashapon.GashaponModule;
import com.yanqu.road.server.protocol.Protocol;

import java.util.*;

/**
 * @Author : Cxp
 * @Time : 2024-10-21
 * @Description : 幸运扭蛋-选择奖励
 */

@Cmd(code = Protocol.S_GASHAPON_CHOOSE_REWARD, desc = "选择奖励")
public class GashaponChooseRewardCmd extends AbstractCommand {
    @Override
    public boolean parseData(YanQuMessage packet) throws Exception {
        return true;
    }

    @Override
    public void execute(GamePlayer player, YanQuMessage packet) throws Exception {
        GashaponProto.GashaponChooseRewardReq req = GashaponProto.GashaponChooseRewardReq.parseFrom(packet.getBytes());

        Map<Integer, Map<Integer, Integer>> chooseMap = new HashMap<>();
        for (GashaponProto.GashaponRewardChoose choose : req.getChooseListList()) {
            int id = choose.getId();
            if (!chooseMap.containsKey(id)) {
                chooseMap.put(id, new HashMap<>());
            }
            chooseMap.get(id).put(choose.getRewardId(), choose.getCnt());
        }
        ret = player.getModule(GashaponModule.class).chooseReward(req.getActivityId(), chooseMap);
    }

    @Override
    public void sendData(GamePlayer player) {
        if (ret != 0) {
            player.sendPacket(Protocol.U_GASHAPON_CHOOSE_REWARD, GashaponProto.GashaponChooseRewardResp.newBuilder().setRet(ret));
        }
    }
}
